package dongbai.soft.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import dongbai.soft.entity.CustomerEntity;
import dongbai.soft.service.CustomerService;
import dongbai.soft.utils.PageUtils;
import dongbai.soft.utils.R;
import dongbai.soft.utils.getUUID;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * @author Kun
 * @email
 * @date 2023-06-21 13:53:47
 */
@RestController
@RequestMapping("")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 列表 ✔
     */
    @RequestMapping("/info")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = customerService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 新增 ✔
     */
    @RequestMapping("/add")
    public R save(@RequestBody CustomerEntity customer) {
        customer.setCid(getUUID.get6UUID());
        customer.setStatus(true);
        return customerService.addCustomer(customer);
    }

    /**
     * 修改 ✔
     */
    @RequestMapping("/edit")
    //@RequiresPermissions(":customer:update")
    public R update(@RequestBody CustomerEntity customer) {
        return customerService.editCustomer(customer);
    }

    /**
     * 删除 ✔
     */
    @RequestMapping("/delete")
    //@RequiresPermissions(":customer:delete")
    public R delete(@RequestBody Map<String, String> cid) {
        return customerService.deleteCustomer(cid.get("cid"));
    }


    /**
     * 添加客户的订单列表
     */
    @RequestMapping("/addCorder")
    public R addCorder(@RequestBody Map<String, String> map) {
        CustomerEntity c = customerService.getById(map.get("CID"));
        String oid = map.get("OID");
        if (StringUtils.isEmpty(c.getOrders())) {
            c.setOrders(oid);
        } else {
            c.setOrders(c.getOrders() + "," + oid);
        }

        customerService.update(new UpdateWrapper<CustomerEntity>().eq("CID", c.getCid()).set("orders", c.getOrders()));
        return R.ok();
    }


}
